package com.xtpt.disaster.utils;

import com.xtpt.disaster.bean.NodeInfo;
import com.xtpt.disaster.common.consts.OSTypeEenum;
import com.xtpt.disaster.service.NodeInfoManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.xtpt.disaster.utils.ServerPathUtil.TrimPathEndSeparator;

/**
 * @author jack_xin
 * @ClassName ParseFileAbsolutePath
 * @Desc 解析文件绝对路径，得到带后缀名的文件名
 * @date 2021年11月17日 13:33
 * @Version TODO
 */
@Component
public class ParseFileAbsolutePath {
    @Autowired
   NodeInfoManageService nodeInfoManageService;
    /**
     * @Author jack xin
     * @Method Name parseFilePath
     * @Description 从绝对路径中提取出带文件后缀名的文件名
     * @Param [clientFilePath(文件的绝对路径), nodeId(节点的id)]
     * @return java.lang.String
     * @Date 13:44 2021/11/17
    **/
    public String parseFilePath(String filePath, Long nodeId) {
        //NodeInfoManageService nodeInfoManageService = new NodeManageServiceImpl();
        NodeInfo nodeInfo = nodeInfoManageService.selectById(nodeId);
        Integer osType = nodeInfo.getOsType();
        int index = 0;
        filePath= TrimPathEndSeparator(filePath);
        filePath=ServerPathUtil.TrimEnd( filePath,'/');
        if (OSTypeEenum.WINDOWS.getValue().equals(osType)) {
            index = filePath.lastIndexOf("\\") + 1;
        }
        else if (OSTypeEenum.LINUX.getValue().equals(osType)){
            index = filePath.lastIndexOf("/") + 1;
        }
        /**
         * @Param filename_suffix 带后缀名的文件名
         */
        return filePath.substring(index);
    }
}
